package cn.glutnn.subject.domain.handler.subject;

import cn.glutnn.subject.common.enums.IsDeletedFlagEnum;
import cn.glutnn.subject.common.enums.SubjectInfoTypeEnum;
import cn.glutnn.subject.domain.convert.SubjectJudgeConverter;
import cn.glutnn.subject.domain.enyity.SubjectAnswerBO;
import cn.glutnn.subject.domain.enyity.SubjectInfoBO;
import cn.glutnn.subject.domain.enyity.SubjectOptionBO;
import cn.glutnn.subject.infra.basic.entity.SubjectJudge;
import cn.glutnn.subject.infra.basic.service.SubjectJudgeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/11/28 下午2:52
 * Description:
 */

@Component
public class JudgeTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        LinkedList<SubjectJudge> subjectJudges = new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(subjectAnswerBO -> {
            SubjectJudge subjectJudge = SubjectJudgeConverter.INSTANCE.convertBoToEntity(subjectAnswerBO);
            subjectJudge.setSubjectId(subjectInfoBO.getId());
            subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectJudges.add(subjectJudge);
        });
        subjectJudgeService.saveBatch(subjectJudges);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        LambdaQueryWrapper<SubjectJudge> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubjectJudge::getSubjectId, subjectId);
        List<SubjectJudge> subjectJudges = subjectJudgeService.list(wrapper);
        SubjectOptionBO optionBO = new SubjectOptionBO();
        LinkedList<SubjectAnswerBO> list = new LinkedList<>();
        subjectJudges.forEach(subjectJudge -> {
            SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
            subjectAnswerBO.setIsCorrect(subjectJudge.getIsCorrect());
            list.add(subjectAnswerBO);
        });
        optionBO.setOptionList(list);
        return optionBO;
    }
}
